package com.zrgj.manager.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.zrgj.domain.Role;
import com.zrgj.domain.User;
import com.zrgj.manager.mapper.RoleMapper;
import com.zrgj.manager.mapper.UserMapper;
import com.zrgj.manager.service.IUserService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Service
@Component
public class UserServiceImpl implements IUserService {
    private UserMapper userMapper;
    private RoleMapper roleMapper;

    public UserServiceImpl(UserMapper userMapper, RoleMapper roleMapper) {
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public User findByName(String username) {
        return userMapper.selectByName(username);
    }

    @Override
    public void register(User user) {
        userMapper.register(user);
    }

    /*@Override
    public org.springframework.security.core.userdetails.User checkName(String username) {
        return null;
    }*/

    /*@Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectByName(username);
        List<Role> roles = roleMapper.findRoleByUserId(user.getUserId());
        user.setRoles(roles);

        return new org.springframework.security.core.userdetails.User(user.getUserName(),"{noop}" + user.getPassword(),getAuthority(roles));
    }

    private Collection<? extends GrantedAuthority> getAuthority(List<Role> roles) {

        List<SimpleGrantedAuthority> list = new ArrayList<>();

        for (Role role:roles){
            list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
        }

        return list;
    }*/
}
